import { axiosSdk } from '@/utils/axiosSdk'
import { productApi, updateApi } from '@/config'
import { sdkRequest } from '@/utils/sdkReuqest'
import { Message } from 'element-ui'
import Axios from 'axios'

export function downloadFile(param) { // 文件下载
	return axiosSdk(
		'get',
		`/${updateApi}/download.do?fid=${param.fid}`
	)
}

export function fundFileUpload(params) { // 基金文件上传
	const form = new FormData()
	form.append('file', params.file)
	form.append('filename', params.file.name)
	form.append('system', 'ItApp-ProductInfo-Management')
	form.append('user', 'admin')
	form.append('fid', '')
	return axiosSdk(
		'post',
		`/${updateApi}/uploadFile.do`,
		form
	)
}

export function uploadFileAndSort(params) { // 其他文件上传
	const form = new FormData()
	form.append('file', params.file)
	form.append('filename', params.file.name)
	form.append('system', 'ItApp-ProductInfo-Management')
	form.append('user', 'admin')
	form.append('fid', '')
	return axiosSdk(
		'post',
		`/${updateApi}/uploadFileAndSort.do`,
		form
	)
}

// 打开文件
export function openFile(fid) {
	const form = new FormData()
	form.append('data', `{"showType":{"save":false,"saveAsPdf":false},"editType":{"readOnly":true},"fid":"${fid}","user":"wangl"}`)
	form.append('type', '3')
	form.append('user', 'wangl')
	const baseurl = process.env.VUE_APP_ENVVARIABLE === 'production' ? 'https://pageoffice.igwfmc.com' : 'http://10.86.130.102:8081'
	// eslint-disable-next-line object-curly-spacing
	Axios.post(`${baseurl}/writeData.do`, form, { headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' } }).then(res => {
		if (res.data.msgCode === 'Y') {
			// eslint-disable-next-line no-undef
			POBrowser.openWindowModeless(`${baseurl}/openFile.do?did=${res.data.obj}`, 'width=1200px;height=800px;')
		} else {
			Message({ type: 'error', message: res })
		}
	})
}

export function editHoverMsg(param) { // 文字提示配置
	return sdkRequest(
		'post',
		`/${productApi}/hover_config/editHover/v1`,
		param
	)
}

export function getHoverMsg(param) { // 获取文字提示
	return sdkRequest(
		'post',
		`/${productApi}/hover_config/get_hover_info/v1`,
		param
	)
}

export function getEnum(param) { // 查询：枚举信息
	return sdkRequest(
		'post',
		`/${productApi}/enum_info/query_enums_info/v1`,
		param
	)
}

export function getCompintroduction(param, page) { // 查询：万德信息
	return sdkRequest(
		'post',
		`/${productApi}/compintroduction/get_compintroduction_list/v1`,
		param,
		page
	)
}

export function dataVerification(params) { // 数据校验
	const form = new FormData()
	form.append('file', params.file)
	form.append('filename', params.file.name)
	form.append('system', 'system')
	form.append('user', 'admin')
	form.append('fid', '')
	return axiosSdk(
		'post',
		`/${productApi}/data/cs_verification/v1`,
		form
	)
}

export function VerificationDataExport(param) { // 校验数据导出
	return sdkRequest(
		'post',
		`/${productApi}/data/export_verification_result/v1`,
		param
	)
}

/** *****************************************************************************/

export function dealIsNull(obj) { // 处理对象中为null的字段，将null转化为""
	for (const key in obj) {
		if (obj[key] == null) {
			obj[key] = ''
		}
	}
	return obj
}

export function formatNumber(value) { // 只能输入数字，可小数，只能有一位小数点，不能出现在第一位
	let msg = ''
	if (value) {
		const arr = [...value]
		if (arr.includes('.')) {
			const index = arr.findIndex(item => item === '.')
			for (let i = 0; i < arr.length; i++) {
				if (i !== index && arr[i] !== '.') {
					msg += arr[i]
				} else if (i === index && i !== 0) {
					msg += '.'
				}
			}
			msg = msg.toString().replace(/[^\d\.]/g, '')
		} else {
			msg = value.toString().replace(/[^\d\.]/g, '')
		}
	}
	return msg
}

export function isEmpty(msg) {
	// Java String 类中 isEmpty() 方法判断字符串的长度是否为空，如果字符串长度为 0，则返回 true，否则返回 false。
	if (Object.prototype.toString.call(msg).substring(8, Object.prototype.toString.call(msg).length - 1) === 'String') {
		return ![...msg].length
	}
	return true
}

export function difftime(startTime, endTime, unit) { // 计算时间
	const start = new Date(startTime)
	const end = new Date(endTime)
	// 计算时间戳的差
	const diffValue = end - start
	// 获取年
	const startYear = start.getFullYear()
	const endYear = end.getFullYear()
	// 获取月
	const startMouth = start.getMonth() + 1
	const endMouth = end.getMonth() + 1
	// 获取日
	const startDay = start.getDate()
	const endDay = end.getDate()
	// 获取小时
	const startHours = start.getHours()
	const endHours = end.getHours()
	// 获取分
	const startMinutes = start.getMinutes()
	const endMinutes = end.getMinutes()
	// 获取秒
	const startSeconds = start.getSeconds()
	const endSeconds = end.getSeconds()
	if (unit === 'y' || unit === 'M') {
		// 相差年份月份
		const diffYear = endYear - startYear
		// 获取当前月天数
		const startDays = getDays(startMouth, startYear)
		const endDays = getDays(endMouth, endYear)
		const diffStartMouth = (startDays - (startDay + ((startHours * 60 + startMinutes + startSeconds / 60) / 60 / 24) - 1)) / startDays
		const diffEndMouth = (endDay + ((endHours * 60 + endMinutes + endSeconds / 60) / 60 / 24) - 1) / endDays
		const diffMouth = diffStartMouth + diffEndMouth + (12 - startMouth - 1) + endMouth + (diffYear - 1) * 12
		if (unit === 'y') {
			return Math.floor(diffMouth / 12 * 100) / 100
		} else {
			return diffMouth
		}
	} else if (unit === 'd') {
		const d = parseInt(diffValue / 1000 / 60 / 60 / 24)
		return d
	} else if (unit === 'h') {
		const h = parseInt(diffValue / 1000 / 60 / 60)
		return h
	} else if (unit === 'm') {
		const m = parseInt(diffValue / 1000 / 60)
		return m
	} else if (unit === 's') {
		const s = parseInt(diffValue / 1000)
		return s
	} else {
		this.$message({ type: 'error', message: '请输入正确的单位', customClass: 'el-messageClass' })
	}
}

// 判断当前月天数
function getDays(mouth, year) {
	let days = 30
	if (mouth === 2) {
		days = year % 4 === 0 ? 29 : 28
	} else if (mouth === 1 || mouth === 3 || mouth === 5 || mouth === 7 || mouth === 8 || mouth === 10 || mouth === 12) {
		days = 31
	}
	return days
}

export function jsonArray(list) { // 将数组序列化
	return JSON.parse(JSON.stringify(list))
}
